<template>
  <header :class="showHead ? 'backWhith' : ''">
    <h2 @click="toHome">张林的个人博客</h2>
    <div class="links">
      <span @click="toHome">首页</span>
    </div>
  </header>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, toRefs, onMounted } from "vue";
import { useRouter } from "vue-router";

export default defineComponent({
  name: "",
  components: {},
  setup() {
    let showHead = ref(false);
    let router = useRouter();

    onMounted(() => {
      let n = window.scrollY;
      window.onscroll = () => {
        if (window.scrollY < n) {
          showHead.value = true;
        }
        if (window.scrollY === 0) {
          showHead.value = false;
        }
        n = window.scrollY;
      };
    });

    function toHome() {
      router.push("/");
    }

    return {
      showHead,
      toHome,
    };
  },
});
</script>

<style scoped lang="scss">
header {
  width: 100vw;
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  padding: 0.5em;
  transition: all 0.5s;
  display: flex;
  justify-content: space-between;
  h2 {
    user-select: none;
    font-size: 20px;
    color: white;
    cursor: pointer;
  }
  .links {
    margin-right: 2rem;
    span {
      user-select: none;
      display: inline-block;
      margin-left: 2rem;
      font-size: 12px;
      font-weight: bold;
      color: white;
      cursor: pointer;
    }
  }
}
.backWhith {
  transition: all 1s;
  background-color: rgba(150, 90, 90, 0.5);
}
</style>
